package com.spi.soconversion.orders;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import com.spi.soconversion.base.SOCException;
import com.spi.soconversion.util.DBConnection;
import com.spi.soconversion.util.ErrorConstants;
import com.spi.soconversion.util.ErrorMessageReader;
import com.spi.soconversion.util.QueryConstants;
import com.spi.soconversion.util.QueryReader;
import com.spi.soconversion.util.SOCConstants;
import com.spi.soconversion.util.SOCFileWriter;
import com.spi.soconversion.util.SOCUtility;

public class SOCPOSProvince {
	
	private static HashMap<String,String> storeProvinceMap = new HashMap<String,String>();
	private String d_logFileName = null;
	private SOCFileWriter d_fileWriter = null;
	
	public SOCPOSProvince(String logFileName, SOCFileWriter fileWriter)
	{
		d_logFileName = logFileName;
		d_fileWriter = fileWriter;
	}
	
	public void populateStoreProvincePairs() throws SOCException
	{
		String posProvinceQuery = QueryReader.getQuery(QueryConstants.GET_POS_STORE_PROVINCE_QUERY);
        	
		Statement stmt=null;
    	ResultSet rsInfo=null;
    	d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(SOCConstants.MESSAGE_FETCH_POS_STORE_NUM_PROVINCE_PAIRS_STARTS));
    	try
		{
    		stmt=DBConnection.getConnection().createStatement();
    		rsInfo=stmt.executeQuery(posProvinceQuery);
			if(rsInfo!=null)
			{
				while(rsInfo.next())
				{
					String posStoreNumber = rsInfo.getString("POS_STORE_NUM");
					String posProvince = rsInfo.getString("POS_PROVINCE");
					int storeNum = 0;
					if(SOCUtility.isNumber(posStoreNumber))
					{
						storeNum = Integer.parseInt(posStoreNumber);
						storeProvinceMap.put(String.valueOf(storeNum),posProvince);
					}	
				}
				
			}			
		}
		catch(SQLException sqle)
		{
			d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(ErrorMessageReader.getError(
    				ErrorConstants.FAILED_FETCH_POS_STORE_NUMBER_PROVINCE_NAMES)+SOCConstants.NEW_LINE+sqle.getMessage()));
			throw new SOCException(
            		ErrorMessageReader.getError(
            				ErrorConstants.FAILED_FETCH_POS_STORE_NUMBER_PROVINCE_NAMES) ,sqle);
    		
		}	
		d_fileWriter.write(d_logFileName, SOCUtility.writeLogMessage(SOCConstants.MESSAGE_FETCH_POS_STORE_NUM_PROVINCE_PAIRS_ENDS));
	}
	
	public static HashMap<String,String> getPOsStoreNumProvinceMap()
	{
		return storeProvinceMap;
	}
}
